const app = getApp();
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collectData: [],//收藏的活动id数组
  },

  //获取活动数据
  getActivityData: function () {
    let that = this;
    that.setData({
      loading: true
    })
    //加载信息
    util.request(api.ACTIVITY_DETAIL + '/' + that.data.id).then(function (res) {
      if (res.success === true) {
        let data = new Object();
        data.activity = res.result;
        that.setData(data);
      }
    });

  },

  //收藏/取消收藏
  collect: function (e) {
    let id = e.currentTarget.dataset.id;
    let that = this;
    let arr = that.data.collectData;
    let index = arr.findIndex(item => item === id);
    if (index === -1) {
      //不存在 ---收藏
      arr.push(id)
      util.request(api.ACTIVITY_COLLECT + '/' + id).then(function (res) {

      });
    } else {
      //存在  ---取消收藏
      arr.splice(index, 1)
      util.request(api.ACTIVITY_COLLECT_CANCEL + '/' + id).then(function (res) {

      });
    }
    that.setData({
      'collectData': arr
    })
  },
  //获取用户收藏信息
  getCollect() {
    let that = this;
    util.request(api.ACTIVITY_GET_COLLECT).then(function (res) {
      if (res.success === true) {
        let data = new Object();
        data.collectData = res.result;
        that.setData(data);
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData(options)
    wx.getSystemInfo({
      success(res) {
        that.setData({ wh: res.windowHeight })
      }
    })
    this.getActivityData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCollect();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})